package com.zq.it.service.impl.admin;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.zq.it.pojo.Course;
import com.zq.it.service.admin.CourseService;
import com.zq.it.service.common.BaseServiceImpl;
import com.zq.it.tool.Constant;

public class CourseServiceImpl extends BaseServiceImpl implements CourseService {

	private List<Course> COURSELIST; //存储查询数据
	@Override
	public List<Course> getAllCourse() {
		
		// TODO Auto-generated method stub
		return getCourseDao().findCourseEtyList();
	}

	@Override
	public void removeCourse(Integer id) {
		getCourseDao().deleteCourseEtyByID(id);
		// TODO Auto-generated method stub
	
	}

	@Override
	public void renewalCourse(Course course) {
        getCourseDao().updateCourseEty(course);
	}

	@Override
	public List getCourse(Integer id) {
		return getCourseDao().findCourseEtyById(id);
	}

	@Override
	public void addCourse(Course course){	
		getCourseDao().saveCourseEty(course);
		// TODO Auto-generated method stub
		
	}

	@SuppressWarnings("deprecation")
	@Override
	public List getCourseListByType(String type,String parameter) {
		if(type.equals("0"))
			getCourseDao().findCourseEtyById(Integer.parseInt(parameter));
		if(type.equals("1"))
			getCourseDao().findCourseEtyByName(parameter);
//		if(type.equals("2"))
//			getCourseDao().findCourseEtyByDate(new Date(parameter));
			
		// TODO Auto-generated method stub
		return COURSELIST;
	}

	@Override
	public List getCourseByExmp(Course course){
		// TODO Auto-generated method stub
		return getCourseDao().findCourseEtyByExmp(course);
	}

	@Override
	public void removeCourse(Course course) {
		// TODO Auto-generated method stub
		getCourseDao().deleteCourseEtyByObj(course);
	}

	@Override
	public void removeAllCourse(int [] ids) {
		getCourseDao().deleteAllEty(getListFromTemplate(ids));
		// TODO Auto-generated method stub
		
	}
	private List getListFromTemplate(int [] ids){
		System.out.println(ids[0]);
		List list=new ArrayList();		
	  for(Course t: COURSELIST){
		 for(int i=0;i<ids.length;i++)
			 if(t.getCourseId().equals(ids[i]))
		        list.add(t);
	   }
	  return list;
	}
	@Override
	public List<Course> importCourseList(File courseFile){
		  List<Course> courseList=getCourseListFromStr(importxlsxFile(courseFile));
		  List<Course> existCourseList=new ArrayList<Course>();
		  
		 for(int i=0;i<courseList.size();i++){		
			 Course course=courseList.get(i);
			 if(!getCourseDao().findCourseEtyByName(course.getCourseName()).isEmpty())
				 existCourseList.add(course);
		 }
		     courseList.removeAll(existCourseList);
		   if(!courseList.isEmpty())
		    	getCourseDao().saveCourseEtyList(courseList);
			return existCourseList;
		}	
	
		 private  List<Course> getCourseListFromStr(String str){		 
			List<Course> list=new ArrayList<Course>();
			String [] firstArray=str.split(";");
			for(int i=0;i<firstArray.length;i++){
				String [] secondArray=firstArray[i].split(":");				
				list.add(new Course(secondArray[0],Constant.getDateFromStr(secondArray[1]),secondArray[2]));
			}
			//System.out.println("大小："+list.size());
			return list;
		}
		 
	//数据的导出
	@Override
		public ByteArrayInputStream exportCourseList(String[] headTitle,List<Course> course) {
			// TODO Auto-generated method stub
			return new ByteArrayInputStream(exportToExcel(course,headTitle,com.zq.it.tool.HeadTitle.EN_COURSEHEAD.getArray(),"com.zq.it.pojo.Course").toByteArray());
		}

	@Override
	public List getCourseListByHQL(Course course){
		StringBuilder builder=new StringBuilder("from Course where state='"+course.getState()+"'");
		//System.out.println(course.getCourseId()+course.getCourseName());
		if(course.getCourseId()!=null)
			builder.append("and courseId="+course.getCourseId());		
		if(course.getCourseName()!=null&&!course.getCourseName().equals(""))
			builder.append("and courseName like'%"+course.getCourseName()+"%'");
		if(course.getCreateDate()!=null&&!course.getCreateDate().equals(""))
			builder.append("and createDate='"+course.getCreateDate()+"'");
		
		// TODO Auto-generated method stub
		return getCourseDao().findCourseEtyByHQL(builder.toString());
	}
		
}
